Remove inefficient validation checks in binary search functions #13836
+51
−80
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a severe performance defect in the binary search implementations by removing inefficient validation checks that defeat the purpose of using binary search.
Problem
The validation check
if list(sorted_collection) != sorted(sorted_collection)was present in four functions:binary_searchbinary_search_std_libbinary_search_by_recursionexponential_searchThis validation creates a full list copy of the collection on every function call, which has O(n) time complexity. This completely defeats the purpose of binary search algorithms, which should be O(log n). For large collections, this validation alone becomes more expensive than the search operation itself.
Solution
Removed the validation checks from all four functions. The functions already clearly document in their docstrings that the collection must be sorted in ascending order. This is a documented precondition that callers must satisfy, not something to validate at runtime in performance-critical code.
Impact
Describe your change:
Checklist: